import { Effect, Reducer } from 'umi';

import { queryCurrent } from '@/services/user';
import { message } from 'antd';

// export interface ReplyModel {

// }

export interface ReplyModelState {
  content?: {};
}

export interface ReplyModelType {
  namespace: 'reply';
  state: ReplyModelState;
  effects: {
    fetchCurrent: Effect;
  };
  reducers: {
    saveContent: Reducer<ReplyModelState>;
  };
}

const ReplyModel: ReplyModelType = {
  namespace: 'reply',
  state: {
    content: {},
  },

  effects: {
    *fetchCurrent({ payload }, { call, put }) {
      const { code, data, info } = yield call(queryCurrent, payload);

      if (code) {
        message.info(info);
        return;
      }

      // const { isLeader } = data
      // const authority: string = isLeader ? 'leader' : 'user'
      // setAuthority(authority);

      yield put({
        type: 'saveContent',
        payload: data,
      });
    },
  },

  reducers: {
    saveContent(state, { payload }) {
      return {
        ...state,
        content: { ...payload } || {},
      };
    },
  },
};

export default ReplyModel;
